package org.zjx.analysis.inventor;

import lombok.Getter;
import org.zjx.core.IWindow;
import org.zjx.entity.ParagraphBean;
import org.zjx.enums.FontType;
import org.zjx.identify.PaneUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;

public class ItemDescriptionAnalysis {
    private final IWindow window;
    private final BufferedImage screenshot;
    @Getter
    private final Rectangle descriptionRectangle;

    public ItemDescriptionAnalysis(IWindow window,BufferedImage screenshot) {
        this.window = window;
        this.screenshot = screenshot;
        this.descriptionRectangle = getPaneArea();
    }

    private Rectangle getPaneArea(){
        PaneUtil paneUtil = new PaneUtil(screenshot, 5, Collections.singletonList(new Color(248, 252, 248)));
        List<Rectangle> rects = paneUtil.detectRect();
        return rects == null || rects.isEmpty() ? null : rects.get(0);
    }

    public  boolean haveDescription(){
        return descriptionRectangle != null;
    }

    //获取描述
    public String getDescription(){
        if (!haveDescription()) {
            throw new IllegalArgumentException("描述不存在!!!");
        }
        return getItemDescription(Collections.singletonList(new Color(255, 255, 0)));
    }

    public String getItemDescription(List<Color> colorList){
        List<ParagraphBean.Word> wordList = window.execute(op -> op.identifyFont(window, screenshot, descriptionRectangle, colorList, FontType.NPC, 14, false));
        StringBuilder sb = new StringBuilder();
        for (ParagraphBean.Word word : wordList) {
            sb.append(word.getName());
        }
        return sb.toString();
    }
}
